package com.blog.admin.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.blog.admin.service.MenuService;
import com.blog.admin.service.RoleService;
import com.blog.base.controller.BaseController;

/**
 * @todo RoleController
 * @author fg
 * @date 2016年1月28日
 */
@Scope("prototype")
@Controller
@RequestMapping("role")
public class RoleController extends BaseController{

	@Resource
	RoleService roleService;
	@Resource
	MenuService menuService;
	/**
	 * @todo 查询所有角色菜单
	 * @author fg
	 * @date 2016年1月28日
	 */
	@RequiresRoles("superAdmin")
	@RequestMapping("/getAllRole")
	public ModelAndView getRoleList()
	{
		mav.setViewName("admin/role/role_list");
		mav.addObject("roleList", roleService.queryAllRole());
		return mav;
	}
	
	/**
	 * @todo role编辑前的展示
	 * @author fg
	 * @date 2016年1月28日
	 */
	@RequiresRoles("superAdmin")
	@RequestMapping("/preModifyRole")
	public ModelAndView preModifyRole(HttpServletRequest request)
	{
		mav.setViewName("admin/role/role_edit");
		mav.addObject("role", roleService.queryRoleById(Integer.parseInt(request.getParameter("rid"))));
		
		JSONArray jsonArray = menuService.queryMenuByRoleId(Integer.parseInt(request.getParameter("rid")),true);
		mav.addObject("menuJson", jsonArray+"");
		return mav;
	}
	
	/**
	 * @todo 修改角色信息
	 * @author fg
	 * @date 2016年3月15日
	 */
	@RequiresRoles("superAdmin")
	@RequestMapping("/modifyRole")
	@ResponseBody
	public JSONObject modifyRole(@RequestParam("roleId")String roleId,
								 @RequestParam("roleName")String roleName,
			 					 @RequestParam("menuIds")String menuIds)
	{
		try 
		{
			roleService.modifyRoleAndRoleMenuMapping(roleId,roleName, menuIds);
			success = 1;
			msg = "修改成功！";
		} 
		catch (Exception e) 
		{
			success = 1;
			msg = e.toString();
		}
		finally
		{
			json.put("success", success);
			json.put("msg", msg);
		}
		
		return json;
	}
	
	
	/**
	 * @todo 打开添加角色的模态框
	 * @author fg
	 * @date 2016年1月30日
	 */
	@RequiresRoles("superAdmin")
	@RequestMapping("/preAddRole")
	public ModelAndView preAddRole()
	{
		JSONArray jsonArray = menuService.queryMenuByRoleId(null,false);
		mav.addObject("menuJson", jsonArray+"");
		mav.setViewName("admin/role/role_add");
		return mav;
	}
	
	/**
	 * @todo 添加角色
	 * @author fg
	 * @date 2016年1月30日
	 */
	@RequiresRoles("superAdmin")
	@RequestMapping("/addRole")
	@ResponseBody
	public JSONObject addRole(@RequestParam("roleName")String roleName
							 ,@RequestParam("menuIds")String menuIds){
		try
		{
			roleService.addRoleAndRoleMenuMapping(roleName,menuIds);
			msg ="添加成功！";
			success = 1;
		} 
		catch (Exception e)
		{
			e.printStackTrace();
			msg = e.toString();
		}
		finally
		{
			json.put("success", success);
			json.put("msg", msg);
		}
		
		return json;
	}
	
	
	/**
	 * @todo 删除
	 * @author fg
	 * @date 2016年1月30日
	 */
	@RequiresRoles("superAdmin")
	@RequestMapping("/deleteRole")
	@ResponseBody
	public JSONObject deleteRole(@RequestParam("roleId")Integer roleId){
		try
		{
			roleService.removeRole(roleId);
			msg ="删除成功！";
			success = 1;
		} 
		catch (Exception e)
		{
			e.printStackTrace();
			msg = e.toString();
		}
		finally
		{
			json.put("success", success);
			json.put("msg", msg);
		}
		
		return json;
	}
	
	
	
}
